<template>
  <div>
    <div>
      <el-tree
        ref="tree"
        class="filter-tree"
        show-checkbox
        :data="permissionList"
        :props="{ label: 'name' }"
        default-expand-all
        check-strictly
        node-key="id"
      />
    </div>

    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="cancelButton">取 消</el-button>
        <el-button size="small" type="primary" @click="setRolesBtn">确 定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'AssignPermission',
  props: {
    // 所有权限数组
    permissionList: {
      type: Array,
      default: _ => []
    },
    // 角色id
    roleId: {
      type: [Number, String],
      required: true
    },
    // 该角色已拥有的权限
    roleIdsList: {
      type: Array,
      default: _ => []
    }
  },
  data() {
    return {}
  },
  watch: {
    roleIdsList() {
      // 通过 keys 设置目前勾选的节点，使用此方法必须设置 node-key 属性
      // (keys, leafOnly) 接收两个参数，1. 勾选节点的 key 的数组 2. boolean 类型的参数
      this.$refs.tree.setCheckedKeys(this.roleIdsList)
    }
  },
  created() {
    // console.log(this.roleId)
  },
  methods: {
    // 取消按钮
    cancelButton() {
      this.$emit('close')
    },

    // 设置权限按钮
    async setRolesBtn() {
      // 要传的数据 {id:角色id, permIds:[] 所有选中的节点的id组成的数组}
      const permIds = this.$refs.tree.getCheckedKeys()

      this.$emit('confirmPer', { id: this.roleId, permIds })
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
